package com.yesorno.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.yesorno.constants.Constants;
import com.yesorno.model.HttpResponseObject;
import com.yesorno.ui.CustomTitleActivity;
import com.yesorno.utils.HttpUtils;

public class ViewCommentsActivity extends CustomTitleActivity {
	private Button goBack;
	private Button comments;
	RegBtnsClickListeners rbc = new RegBtnsClickListeners();

	private long currentTopicId;

	private ListView view_comment_list;

	/**
	 * 注册页面按钮的监听类
	 * 
	 * @author samoin
	 * @date 2011-07-06
	 * @version 0.1
	 * 
	 */
	class RegBtnsClickListeners implements OnClickListener {
		@Override
		public void onClick(View v) {
			if (v == goBack) {
				Intent intent = new Intent();
				ViewCommentsActivity.this.setResult(RESULT_OK, intent);
				ViewCommentsActivity.this.finish();
			}
			if (v == comments) {
				Intent intent = new Intent();
				//TODO 将来这里到具体的发布评论去
				// intent.setClass(ViewCommentsActivity.this, cls)
				ViewCommentsActivity.this.setResult(RESULT_OK, intent);
				ViewCommentsActivity.this.finish();
			}
		}

	}

	@Override
	public void setTitleViews() {
		LinearLayout titleLayout = getTITLE_LAYOUT();
		goBack = CustomTitleActivity.createTitleLeftButton(
				ViewCommentsActivity.this, R.string.reg_title_btn_goback,
				Constants.TITLE_BTN_MARGIN1);
		goBack.setOnClickListener(rbc);
		titleLayout.addView((View) goBack.getParent());

		TextView tv = CustomTitleActivity.createTitleDesc(
				ViewCommentsActivity.this, R.string.view_comment_title, 0);
		titleLayout.addView((View) tv.getParent());

		comments = CustomTitleActivity.createTitleRightButton(
				ViewCommentsActivity.this, R.string.view_comment_comment,
				Constants.TITLE_BTN_MARGIN2);
		comments.setOnClickListener(rbc);
		LinearLayout ll = (LinearLayout) comments.getParent();
		ll.setGravity(Gravity.RIGHT);
		titleLayout.addView((View) comments.getParent());
	}

	@Override
	public void setLayoutId() {
		setContentView(R.layout.view_comment);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		currentTopicId = getIntent().getLongExtra("currentTopicId", 0);

		view_comment_list = (ListView) findViewById(R.id.view_comment_list);

		// 这句代码非常重要，用来设置点击的当前项的背景
		view_comment_list.setSelector(R.drawable.list_bg);
		// 这句代码非常重要，用来设置拖动中的背景
		view_comment_list.setCacheColorHint(0);

		HttpResponseObject hro = HttpUtils
				.getInfoByUrlPost(Constants.COMMENT_LIST_URL
						+ "currentTopicId=" + currentTopicId,
						ViewCommentsActivity.this);
		JSONObject jsonObject = hro.getResult();
		try {
			JSONArray jsonArray = jsonObject.getJSONArray("list");
			List list = new ArrayList();
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject temp = (JSONObject) jsonArray.get(i);
				Map map = new HashMap();
				map.put("userName", temp.get("userName"));
				map.put("userLogo", temp.get("userLogo"));
				map.put("editTime", temp.get("editTime"));
				map.put("desc", temp.get("desc"));
				map.put("userId", temp.get("userId"));
				map.put("commentType", temp.get("commentType"));
				list.add(map);
			}
			ImgSimpleAdapter adapter = new ImgSimpleAdapter(
					ViewCommentsActivity.this, list, R.layout.comment_list,
					new String[] { "userName", "userLogo", "editTime", "desc",
							"userId", "commentType" }, new int[] {
							R.id.user_name, R.id.user_logo, R.id.edit_time,
							R.id.comment_desc, R.id.user_id,
							R.id.comment_type_btn });
			view_comment_list.setAdapter(adapter);
			view_comment_list
					.setOnItemClickListener(new MyOnItemClickListener());
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	class MyOnItemClickListener implements OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			Intent intent = new Intent();
			TextView idTv = (TextView) arg1.findViewById(R.id.user_id);
			intent.putExtra("currentUserId", Long.valueOf(idTv.getText() + ""));
			intent.setClass(ViewCommentsActivity.this,
					CommentListMenuActivity.class);
			startActivity(intent);
		}

	}

	class ImgSimpleAdapter extends SimpleAdapter {

		public ImgSimpleAdapter(Context context,
				List<? extends Map<String, ?>> data, int resource,
				String[] from, int[] to) {
			super(context, data, resource, from, to);
		}

		@Override
		public void setViewImage(ImageView v, String value) {
			Handler handler = new Handler();
			CustomTitleActivity.BitmapThread r = new CustomTitleActivity.BitmapThread(
					value, v, false);
			handler.post(r);
		}

		@Override
		public void setViewText(TextView v, String text) {
			if (v.getId() == R.id.comment_type_btn) {
				if (text.equals("0")) {
					v.setBackgroundDrawable(getResources().getDrawable(
							R.drawable.common_btn_bg_green));
					v.setTextColor(getResources().getColor(R.color.black));
					text = "Yes";
				}
				if (text.equals("1")) {
					v.setBackgroundDrawable(getResources().getDrawable(
							R.drawable.common_btn_bg_red));
					v.setTextColor(getResources().getColor(R.color.white));
					text = "No";
				}
			}
			if (v.getId() == R.id.edit_time) {
				text = getResources().getString(
						R.string.view_comment_date_start_desc)
						+ text;
			}
			super.setViewText(v, text);
		}

	}
}
